package com.bdqn.bank_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bank_system.mapper.AccountMapper;
import com.bdqn.bank_system.pojo.Account;
import com.bdqn.bank_system.pojo.TransactionRecord;
import com.bdqn.bank_system.service.TransactionRecordService;
import com.bdqn.bank_system.mapper.TransactionRecordMapper;
import com.bdqn.bank_system.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【transaction_record】的数据库操作Service实现
* @createDate 2024-10-31 14:14:36
*/
@Service
public class TransactionRecordServiceImpl extends ServiceImpl<TransactionRecordMapper, TransactionRecord>
    implements TransactionRecordService{
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private TransactionRecordMapper transactionRecordMapper;

    @Override
    public List<TransactionRecord> selectPageRecord(String openDate, String closeDate, Page<TransactionRecord> page) {
        return transactionRecordMapper.selectPageRecord(openDate, closeDate, (page.getPageNo() - 1) * page.getPageSize(), page.getPageSize());
    }

    @Transactional
    @Override
    public int deal(String toCardno, String doCardno, Double amount) {
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.eq("cardno",toCardno);
        // 支出账户
        Account toAccount = accountMapper.selectOne(qw);
        // 支入账户
        qw.eq("cardno",doCardno);
        Account doAccount = accountMapper.selectOne(qw);
        // 修改操作
        // 修改支出账户
        toAccount.setBalance(toAccount.getBalance() - amount);
        accountMapper.updateById(toAccount);
        // 修改支入账户
        doAccount.setBalance(doAccount.getBalance() + amount);
        accountMapper.updateById(doAccount);
        // 添加交易记录
        // 支出账户
        TransactionRecord toRecord = new TransactionRecord();
        toRecord.setCardno(toCardno);
        toRecord.setTransactiondate(DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()));
        toRecord.setAmount(BigDecimal.valueOf(0 - amount));
        toRecord.setBalance(BigDecimal.valueOf(toAccount.getBalance() - amount));
        toRecord.setTransationtype("消费");
        // 支入账户
        TransactionRecord doRecord = new TransactionRecord();
        doRecord.setCardno(doCardno);
        doRecord.setTransactiondate(DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()));
        doRecord.setAmount(BigDecimal.valueOf(amount));
        doRecord.setBalance(BigDecimal.valueOf(toAccount.getBalance() + amount));
        doRecord.setTransationtype("存款");

        boolean toSave = super.save(toRecord);
        boolean doSave = super.save(doRecord);
        if (toSave && doSave){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public Integer countDate(String openDate, String closeDate) {
        return transactionRecordMapper.countDate(openDate, closeDate);
    }
}




